package com.gl.wxstore.sys.service;

import com.gl.wxstore.common.utils.PageConstants;
import com.gl.wxstore.common.utils.PageModel;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

public class ProduceService{
	
	@Before(Tx.class)
	public Page<Record> list(Integer pageNo) {
		String selectSql = "SELECT sp.id,sp.name,sp.description,sp.pic,sp.price ";
    	StringBuilder fromSql = new StringBuilder("FROM store_product sp ");
    	return Db.paginate(pageNo, PageConstants.PAGE_SIZE, selectSql, fromSql.toString());
	}
	
	@Before(Tx.class)
	public Page<Record> listStoreCart(Integer pageNo,String userId) {
		String selectSql = "SELECT sc.id,sp.name AS storeName,sc.count,sp.price AS price ";
    	StringBuilder fromSql = new StringBuilder("FROM store_product sp,store_cart sc "
    			+ "WHERE sc.store_product_id = sp.id AND sc.state = 1 AND sc.sys_user_id=" + userId );
    	return Db.paginate(pageNo, PageConstants.PAGE_SIZE, selectSql, fromSql.toString());
	}
	
	@Before(Tx.class)
	public Integer ttp(PageModel<Record> p){
		Integer ttp = 0;
		for(int i=0;i<p.getList().size();i++){
			ttp += Integer.parseInt(p.getList().get(i).get("count").toString()) * Integer.parseInt(p.getList().get(i).get("price").toString());
		}
		return ttp;
	}
}
